﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sbook.Business;
using Sbook.Data;
using Sbook.Library;
using System.Data;
using System.Data.SqlClient;
namespace Sbook
{
    public partial class ShoppingCart : System.Web.UI.Page
    {
        protected string Uid = "";
        protected string Total = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                
                BindGrid();
                Total = Shopping.ShoppingCart.Instance.Items.Count.ToString();
                if (Mysession.isLogin)
                    Uid = Mysession.Session.UId;
                else
                    Uid = "0";
            }
        }
        private void BindGrid()
        {
            grdShoppingCart.DataSource = Shopping.ShoppingCart.Instance.Items;
            grdShoppingCart.DataBind();
            double tongTien = 0;
            for (int i = 0; i < grdShoppingCart.Items.Count; i++)
            {
                tongTien += Convert.ToDouble(grdShoppingCart.Items[i].Cells[4].Text);
            }
            lblTotal.Text = tongTien.ToString();
        }
        protected static string PId = "";
        protected static int PID = int.MinValue;
        protected static int Quantity = int.MinValue;

        protected void grdShoppingCart_ItemCommand(object sender, DataGridCommandEventArgs e)
        {
            string strCA = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "Edit":
                    PID = Convert.ToInt32(strCA);
                    for (int i = 0; i < grdShoppingCart.Items.Count; i++)
                    {
                        if (grdShoppingCart.Items[i].Cells[6].Text == strCA)
                        {
                            Quantity = Convert.ToInt32(((TextBox)grdShoppingCart.Items[i].FindControl("txtQuantity")).Text);
                            Shopping.ShoppingItem item = Shopping.ShoppingCart.Instance.Items.Find(s => s.ProductId == PID);
                            item.Quantity = Quantity;
                            Shopping.ShoppingCart.Instance.Update(item);
                        }
                    }
                    BindGrid();
                    break;
                case "Delete":
                    Shopping.ShoppingCart.Instance.Remove(Convert.ToInt32(strCA));
                    BindGrid();
                    break;
            }
        }
        protected void lbtEdit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Shopping.ShoppingItem item = new Shopping.ShoppingItem();
                item.ProductId = PID;
                
            }
        }
        protected void lbtDelete_Click(object sender, EventArgs e)
        {

        }
    }
}